<?php
namespace Functions\Model;
use Think\Model;
/**
 * 套系类型操作
 */
class StyleModel extends Model {

    //完成
    protected $_auto = array (

    );

    //验证
    protected $_validate = array(
        array('style_name','require', '请填写套系类型名称'),
        array('service_id','require', '请选择服务类型'),
        array('style_price','require', '请填写套系类型价格'),
    );

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(), $limit = 0){
        $data   = $this->table("__STYLE__ as A")
                    ->join('__SERVICE__ as B ON A.service_id = B.service_id')
                    ->field('A.*,B.service_name')
                    ->where($where)
                    ->limit($limit)
                    ->select();
        return $data;
    }

    /**
     * 获取数量
     * @return int 数量
     */
    public function countList($where = array()){

        return  $this->table("__STYLE__ as A")
                    ->join('__SERVICE__ as B ON A.service_id = B.service_id')
                    ->field('A.*,B.service_name')
                    ->where($where)
                    ->count();
    }
    /**
     * 获取信息
     * @param int $serviceId ID
     * @return array 信息
     */
    public function getInfo($styleId)
    {
    	return $this->where('style_id ='.$styleId)->find();
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {

    }

    /**
     * 更新信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function saveData($type = 'add'){
        
        //事务处理
        $this->startTrans();
        $data = $this->create();
        if(!$data){
            $this->rollback();
            return false;
        }
        if($type == 'add'){
            //写入数据
            $status = $this->add();
            if($status){
                $this->commit();
            }else{
                $this->rollback();
            }
            return $status;
        }
        if($type == 'edit'){
            //修改数据
            $status = $this->where('style_id='.$data['style_id'])->save();
            if($status === false){
                $this->rollback();
                return false;
            }
            $this->commit();
            return true;
        }
        $this->rollback();
        return false;
    }

    /**
     * 删除信息
     * @param int $serviceId ID
     * @return bool 删除状态
     */
    public function delData($styleId)
    {
        $map = array();
        $map['style_id'] = $styleId;
        return $this->where($map)->delete();
    }

}
